/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hadss_adaptive_layout/hadss_adaptive_layout.dart';
import 'package:provider/provider.dart';
import 'package:samples/utils/music_detail_bloc.dart';

import '../main.dart';
import 'music_detail_play_page.dart';
import 'music_detail_text_page.dart';

class MusicDetailPage extends StatefulWidget {
  final Map arguments;

  const MusicDetailPage({Key? key, required this.arguments}) : super(key: key);

  @override
  State<MusicDetailPage> createState() => _MusicDetailPageState();
}

class _MusicDetailPageState extends State<MusicDetailPage> {
  WidthBreakpoint screenType = WidthBreakpoint.xs;
  HeightBreakpoint screenHeightType = HeightBreakpoint.sm;

  final MusicDetailBloc _bloc = MusicDetailBloc();
  bool onPinchStart = false;

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    _bloc.arguments = widget.arguments;
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<RouteManager>(builder: (context, provider, child) {
      screenType = provider.widthBreakpoint;
      screenHeightType = provider.heightBreakpoint;
      return _contentView(context);
    });
  }

  Widget _contentView(BuildContext context) {
    return MultiProvider(
        providers: [
          ChangeNotifierProvider(create: (bloc) => _bloc),
        ],
        child: Scaffold(
          appBar: null,
          body: RawKeyboardListener(
            focusNode:
            FocusNode(debugLabel: 'global_raw_keyboard'), // 确保焦点节点存在且可用
            onKey: (RawKeyEvent event) {
              if (event is RawKeyDownEvent) {
                if (event.logicalKey == LogicalKeyboardKey.escape) {
                  Navigator.pop(context);
                }
                _bloc.refresh(key: event.logicalKey);
              }
            },
            child: Stack(
              children: [
                Transform.scale(
                    scale: 95 / 75,
                    child: Container(
                        width: MediaQuery.of(context).size.width,
                        height: MediaQuery.of(context).size.height,
                        decoration: const BoxDecoration(
                          color: Color(0xFF3E1513),
                          image: DecorationImage(
                            scale: 1.2,
                            image: AssetImage(
                                'lib/assets/images/title_page_bg.png'), // 图片路径
                            fit: BoxFit.fitHeight, // 填充方式
                          ),
                        ))),
                LayoutBuilder(builder: (context, constraints) {
                  return buildDetailPlayPage();
                }),
              ],
            ),
          ),
        ));
  }

  Widget buildDetailPlayPage() {
    return Container(
      margin: const EdgeInsets.only(top: 10, bottom: 48),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              InkWell(
                onTap: () {
                  Navigator.pop(context);
                },
                child: Container(
                  padding: const EdgeInsets.only(left: 20, right: 20, top: 40),
                  child: Image.asset('lib/assets/images/top_left.png'),
                ),
              ),
              Container(
                padding: const EdgeInsets.only(left: 20, right: 20, top: 40),
                child: Image.asset('lib/assets/images/top_right.png'),
              ),
            ],
          ),
          isShowDoubleScreen()
              ? Expanded(
                  child: Row(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    Expanded(
                      child: MusicDetailPlayerPage(
                        isNeedPlay: widget.arguments["isPlay"] == null
                            ? false
                            : widget.arguments["isPlay"] as bool,
                      ),
                    ),
                    const Expanded(
                      child: MusicDetailTextPage(),
                    ),
                  ],
                ))
              : Expanded(
                  child: MusicDetailPlayerPage(
                  isNeedPlay: widget.arguments["isPlay"] == null
                      ? false
                      : widget.arguments["isPlay"] as bool,
                )),
        ],
      ),
    );
  }

  bool isShowDoubleScreen() {
    bool isShow = false;
    if (screenType != WidthBreakpoint.xs && screenType != WidthBreakpoint.sm) {
      if (screenType == WidthBreakpoint.md &&
          screenHeightType == HeightBreakpoint.md) {
        isShow = true;
      } else {
        Orientation orientation = MediaQuery.of(context).orientation;
        if (orientation == Orientation.landscape) {
          isShow = true;
        }
      }
    }
    return isShow;
  }
}
